package com.tianji.aigc.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.annotation.NacosConfigListener;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import io.netty.util.internal.shaded.org.jctools.queues.atomic.AtomicReferenceArrayQueue;
import jakarta.annotation.PostConstruct;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

/**
 * srf 自己写一遍代码
 * 作业
 */
@Slf4j
@Getter
@Configuration
@RequiredArgsConstructor
public class SystemPromptConfig {
    private final NacosConfigManager nacosConfigManager;
    private final AIProperties aiProperties;

    // 使用原子引用，保证线程安全
    private final AtomicReference<String> chatSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> titleSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> routeAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> recommendAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> buyAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> consultAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> knowledgeAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> textSystemMessage = new AtomicReference<>();


    @PostConstruct
    public void init(){
        //读取天机助手提示词配置文件
        loadConfig(aiProperties.getSystem().getChat(),chatSystemMessage);
        //读取根据会话内容生成标题配置文件
        loadConfig(aiProperties.getSystem().getTitle(),titleSystemMessage);
        //读取路由智能体系统提示词配置文件
        loadConfig(aiProperties.getSystem().getRouteAgent(), routeAgentSystemMessage);
        //读取推荐智能体系统提示词配置文件
        loadConfig(aiProperties.getSystem().getRecommendAgent(), recommendAgentSystemMessage);
        //读取购买智能体系统提示词配置文件
        loadConfig(aiProperties.getSystem().getBuyAgent(), buyAgentSystemMessage);
        //读取咨询智能体系统提示词配置文件
        loadConfig(aiProperties.getSystem().getConsultAgent(), consultAgentSystemMessage);
        //读取知识讲解智能体系统提示词配置文件
        loadConfig(aiProperties.getSystem().getKnowledgeAgent(), knowledgeAgentSystemMessage);
        //读取文本提示语配置文件
        loadConfig(aiProperties.getSystem().getText(), textSystemMessage);

    }

    private void loadConfig(AIProperties.System.Chat chatConfig, AtomicReference<String> target) {
        try{
            var dataId = chatConfig.getDataId();
            var group = chatConfig.getGroup();
            var timeoutMs = chatConfig.getTimeoutMs();
            var config = nacosConfigManager.getConfigService().getConfig(dataId,group,timeoutMs);
            target.set(config);
            log.info("读取{}成功，内容为：{}",target,config);

            nacosConfigManager.getConfigService().addListener(dataId,group,new Listener(){
                @Override
                public Executor getExecutor() {
                    return null;
                }
                @Override
                public void receiveConfigInfo(String info) {
                    target.set(info);
                    log.info("配置{}更新，内容为：{}",target,info);
                }
            });
        } catch (NacosException e) {
            log.error("加载配置失败",e);
        }
    }

}
